package jjn.round1;

/**
 * @author Jjn
 * @since 2022/9/16 22:47
 */
public class LeetCode547_NumberOfProvinces {
    public int findCircleNum(int[][] isConnected) {
        int cities = isConnected.length;
        int total = 0;
        boolean[] visited = new boolean[cities];
        for (int j = 0; j < cities; j++) {
            if (visited[j]) {
                continue;
            }
            flood(isConnected, visited, j);
            total++;
        }
        return total;
    }

    private void flood(int[][] isConnected, boolean[] visited, int i) {
        for (int k = 0; k < isConnected.length; k++) {
            if (isConnected[i][k] == 1 && !visited[k]) {
                visited[k] = true;
                flood(isConnected, visited, k);
            }
        }
    }
}
